﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Model;

namespace TanHungLong
{
    public partial class FrmQuyDoi : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        public static QuyDoiBOCollection quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
        private QuyDoiBOCollection tempBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();

        #endregion variant

        //=====================================
        #region form
        public FrmQuyDoi(QuyDoiBOCollection qdBOColl)
        {
            InitializeComponent();
            tempBOColl = qdBOColl;
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmQuyDoi_Load);
            this.Shown += new EventHandler(FrmQuyDoi_Shown);
            this.KeyDown +=new KeyEventHandler(FrmQuyDoi_KeyDown);

            this.btnChon.Click += new EventHandler(btnChon_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
        }

        private void FrmQuyDoi_Shown(object sender, EventArgs e)
        {
            this.grdViewQuyDoi.Focus();
        }

        private void FrmQuyDoi_Load(object sender, EventArgs e)
        {

            Utility.InitControl(this.reItemLookUpEditDonViTinh);
            Utility.InitControl(this.reItemLookUpEditMatHang);

            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid

        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            this.bdSource.DataSource = this.tempBOColl.Clone();
            
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reItemLookUpEditDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reItemLookUpEditMatHang, matHangBOList, matHangBOList.Count);

            #endregion BOList

        }

        private void LayQuyDoi()
        {
            quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();

            this.tempBOColl = (QuyDoiBOCollection)this.bdSource.DataSource;
            int rowcountMV = tempBOColl.Count;
            if (rowcountMV > 0)
                for (int i = 0; i < rowcountMV; i++)
                {
                    if (tempBOColl[i].DELE.Value)
                    {
                        QuyDoiBO tpQD = tempBOColl[i];
                        quyDoiBOColl.Add(tpQD);
                    }
                }
        }

        #endregion function

        private void FrmQuyDoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnChon_Click(null, null);
            }

            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
            {
                quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
                this.Close();
            }

        }


        void btnChon_Click(object sender, EventArgs e)
        {
            try
            {
                LayQuyDoi();
                this.Close();
            }
            catch { };
        }


    }
}